package com.ruoyi.project.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.project.system.domain.Promotion;
import com.ruoyi.project.system.domain.SmProduct;
import com.ruoyi.project.system.domain.vo.Option;
import com.ruoyi.project.system.mapper.SmCustomizedPriceMapper;
import com.ruoyi.project.system.mapper.SmProductMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.system.service.ISmProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品Service业务层处理
 */
@Service
public class SmProductServiceImpl implements ISmProductService {

    @Autowired
    private SmProductMapper smProductMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SmCustomizedPriceMapper smCustomizedPriceMapper;

    @Override
    public List<Option> selectOption() {
        List<Option> options = smProductMapper.selectId();   // 各产品邀请查询
        options.add(new Option("主页轮播图", "swiper"));    // 轮播图
//        options.add(new Option("队长邀请队员海报", "captain->member"));    // 队长邀请队员
//        options.add(new Option("区域队长邀请队员海报", "region->member"));    // 区域队长邀请队员
//        options.add(new Option("区域队长邀请队长海报", "region->captain"));    // 区域队长邀请队长
        options.add(new Option("邀请下级海报", "member"));     // 邀请队员
        options.add(new Option("邀请代理海报", "captain"));    // 邀请队长
        options.add(new Option("平台宣传海报", "platform"));           // 平台宣传海报
        options.add(new Option("客服二维码", "service"));           // 平台宣传海报
        return options;
    }

    @Override
    public String inviteCode(Long userId, String info) {
        String inviteCode = userMapper.getInviteCode(userId, "REG", info);
        if (StringUtils.isEmpty(inviteCode)) {
            inviteCode = IdUtils.fastSimpleUUID().substring(0, 6);
            userMapper.insertInviteCode(userId, "REG", info, inviteCode);
        }
        return inviteCode;
    }

    @Override
    public Map<String, Object> index() {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, String>> PRO = smProductMapper.homePageProducts("1");
        result.put("PRO", PRO);
        result.put("more", smProductMapper.productCount() > PRO.size());
        return result;
    }

    @Override
    public BigDecimal userBasePrice(Long userId, Integer roleId, SmProduct smProduct) {
        if (roleId == 2 || roleId == 3 || roleId == 4) {
            if (roleId == 2) {
                // 总代底价
                BigDecimal zddzdj = smCustomizedPriceMapper.queryAmount(userId, smProduct.getId(), "ZDDZDJ");
                return zddzdj != null ? zddzdj : smProduct.getRegionPricing();
            } else if (roleId == 3) {
                Long parentId = userMapper.queryParentId(userId);
                if (parentId != null){
                    // 总代底价
                    BigDecimal zddj = userBasePrice(parentId, 2, smProduct);
                    BigDecimal zdxjtc = smCustomizedPriceMapper.queryAmount(parentId, smProduct.getId(), "XJTC");
                    return zddj.add(zdxjtc != null ? zdxjtc : smProduct.getBasePriceAdd());
                } else {
                    return smProduct.getCaptainPricing();
                }
            } else if (roleId == 4) {
                Long parentId = userMapper.queryParentId(userId);
                // 总代下级 底价
                BigDecimal zdxjdj = userBasePrice(
                        parentId,
                        userMapper.queryRoleId(parentId),
                        smProduct
                );
                // 总代下级 提成
                BigDecimal zdxxjtc = smCustomizedPriceMapper.queryAmount(parentId, smProduct.getId(), "XJTC");
                return zdxjdj.add(zdxxjtc != null ? zdxxjtc : smProduct.getBasePriceAdd());
            } else {
                return null;
            }
        } else {
            return smProduct.getGeneralAccountPricing();
        }
    }

    /**
     * 查询某用户所有在展产品基础信息，底价（自用价），推广收益
     *
     * @return
     */
    @Override
    public List<Promotion> promotion() {
        Long userId = SecurityUtils.getUserId();
        Integer roleId = userMapper.queryRoleId(userId);
        List<SmProduct> smProducts = smProductMapper.selectSmProductList_();
        List<Promotion> promotions = new ArrayList<>();
        for (SmProduct smProduct : smProducts) {
            promotions.add(
                    new Promotion(
                            smProduct,
                            userBasePrice(userId, roleId, smProduct),
                            promotionAdd(userId, smProduct)
                    )
            );
        }
        return promotions;
    }

    /**
     * 查询某用户对某产品推广收益
     *
     * @param userId
     * @param smProduct
     * @return
     */
    @Override
    public BigDecimal promotionAdd(Long userId, SmProduct smProduct) {
        BigDecimal amount = smCustomizedPriceMapper.queryAmount(userId, smProduct.getId(), "TGSY");
        return amount != null ? amount : smProduct.getPromotionAdd();
    }

    /**
     * 查询某用户对某产品推广价格
     *
     * @param userId
     * @param smProduct
     * @param roleId
     * @return
     */
    @Override
    public BigDecimal promotionPrice(Long userId, Integer roleId, SmProduct smProduct) {
        return userBasePrice(userId, roleId, smProduct).add(promotionAdd(userId, smProduct));
    }

    @Override
    public BigDecimal basePriceAdd(Long userId, SmProduct smProduct) {
        BigDecimal amount = smCustomizedPriceMapper.queryAmount(userId, smProduct.getId(), "XJTC");
        return amount != null ? amount : smProduct.getBasePriceAdd();
    }

    @Override
    public BigDecimal maxLimit(Long userId, SmProduct smProduct, String type) {
        BigDecimal amount = smCustomizedPriceMapper.queryAmount(userId, smProduct.getId(), type);
        if (type.indexOf("XJTC") != -1) {
            amount = amount == null ? smProduct.getMaxBasePriceAdd() : amount;
        } else if (type.indexOf("TGSY") != -1) {
            amount = amount == null ? smProduct.getMaxPromotionAdd() : amount;
        }
        return amount;
    }

    /**
     * 查询产品
     *
     * @param id 产品主键
     * @return 产品
     */
    @Override
    public SmProduct selectSmProductById(Integer id) {
        return smProductMapper.selectSmProductById(id);
    }

    /**
     * 修改产品
     *
     * @param smProduct 产品
     * @return 结果
     */
    @Override
    public int updateSmProduct(SmProduct smProduct) {
        smProduct.setUpdateTime(DateUtils.getNowDate());
        return smProductMapper.updateSmProduct(smProduct);
    }

}
